<template>
  <!-- 电流和炉阻 -->
  <div id="furnaceAnd-card"></div>
</template>

<script>
export default {
  name: "furnaceAnd-card",
  props: {
    chartsData: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      frist: true,
    };
  },
  watch: {
    chartsData: {
      handler(newData) {
        this.initEchart(newData);
      },
    },
  },
  methods: {
    initEchart(newData) {
      try {
        this.myChart.dispose();
      } catch (e) {
        // console.log(e)
      }
      console.log(newData, "目前");
      const chartDom = document.getElementById("furnaceAnd-card");
      const myChart = this.$echarts.init(chartDom);
      let option;
      option = {
        color: ["#00BBF7", "#06FDBC", "#FD8601"],
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            crossStyle: {
              color: "#999",
            },
          },
          padding: 3,
          textStyle: {
            fontSize: 10,
            lineHeight: 15,
          },
        },
        legend: {
          data: ["炉阻", "电流", "I²R"],
          textStyle: {
            color: "#B4B4B4",
          },
          top: "0%",
          icon: "rect",
          right: 20,
          itemWidth: 8,
          itemHeight: 8,
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          axisTick: {
            show: false,
          },
          axisLabel: {
            interval: "auto",
            formatter: function (value) {
              return value.slice(11, 16);
            },
            textStyle: {
              fontSize: 8,
            },
          },
        },
        yAxis: [
          {
            name: "炉阻(R)",
            type: "value",
            axisLine: {
              show: true,
            },
            splitLine: { show: true },
            axisTick: {
              show: false,
            },
            splitNumber: 6,
          },
          {
            name: "I",
            type: "value",
            axisLine: {
              show: true,
            },
            splitLine: { show: true },
            axisTick: {
              show: true,
            },
            splitNumber: 6,
            offset: 0,
          },
          {
            name: "I²R",
            type: "value",
            axisLine: {
              show: true,
            },
            splitLine: { show: true },
            axisTick: {
              show: true,
            },
            splitNumber: 6,
            offset: 34,
          },
        ],
        series: [
          {
            name: "炉阻",
            type: "line",
            smooth: true,
            showAllSymbol: false,
            symbol: "circle",
            symbolSize: 3,
            xAxisIndex: 0,
            yAxisIndex: 0,
            barWidth: 4,
          },
          {
            name: "电流",
            type: "line",
            smooth: true,
            showAllSymbol: false,
            symbol: "circle",
            symbolSize: 3,
            xAxisIndex: 0,
            yAxisIndex: 1,
            encode: {
              x: 0,
              y: 2,
            },
          },
          {
            name: "I²R",
            type: "line",
            smooth: true,
            showAllSymbol: true,
            symbol: "circle",
            symbolSize: 2,
            xAxisIndex: 0,
            yAxisIndex: 2,
            lineStyle: {
              width: 1.5,
            },
            encode: {
              x: 0,
              y: 3,
            },
          },
        ],
        dataset: {
          source: newData,
        },
      };
      if (this.frist) {
        option && myChart.setOption(option);
        this.frist = false;
      } else {
        this.updateEchartData(myChart, newData);
      }
    },
    updateEchartData(myChart, data) {
      const option = {
        dataset: {
          source: data, // 数组
        },
      };
      myChart.setOption(option);
    },
  },
};
</script>

<style  scoped>
#furnaceAnd-card {
  width: 100%;
  height: 250px;
}
.card {
  background-color: #ffffff;
}
.head {
  height: 40px;
  display: flex;
  align-items: center;
  padding: 3px 10px;
  margin-top: 14px;
  &-status {
    font-size: 13px;
    margin-left: 150px;
    padding: 7px;
    border-radius: 15px;
    background-color: #f2f2f2;
  }
  &-left {
    width: 5px;
    height: 30px;
    background-color: #01bb70;
  }
}
</style>
